package com.jinfang.bankpractice.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import com.jinfang.bankpractice.dto.ReturnData;

@ControllerAdvice(basePackageClasses = {
		QuestionController.class,HistoryController.class,UserController.class	
})
public class FooControllerAdvice extends ResponseEntityExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
   Object handleControllerException(HttpServletRequest request, Throwable ex) {
        HttpStatus status = getStatus(request);
      //  return new ResponseEntity<>(new CustomErrorType(status.value(), ex.getMessage()), status);
        return new ReturnData(status.value(), "内部错误！" , ex.getMessage(), ex);
    }

    private HttpStatus getStatus(HttpServletRequest request) {
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if (statusCode == null) {
            return HttpStatus.INTERNAL_SERVER_ERROR;
        }
        return HttpStatus.valueOf(statusCode);
    }

}